<?php
/**
 * Created by PhpStorm.
 * User: whs
 * Date: 2018/9/20
 * Time: 17:30
 */

include_once "Client.php";

$server = new swoole_websocket_server("0.0.0.0", 9902);

$server->set([

    'daemonize' => true,
    'worker_num' => 8,
    'log_file' => __DIR__ . '/ceshi.log',
]);


$server->on('open', function (swoole_websocket_server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});

$server->on('message', function (swoole_websocket_server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";

    /**
     * 发送给tcp服务器
     *
     */
    $data = json_decode($frame->data,true);

    if(isset($data['path'])){

        Client::send(json_decode($frame->data,true));

    }else{

        foreach ($server->connections as $fd){

            if($fd !=$frame->fd){

                $server->push($fd, $frame->data);

            }

        }

    }


});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();